﻿var zombies = zombies || {};

zombies.ListProductClient = function () {
    var self = this;

    //product info
    self.PId = ko.observable();
    self.Name = ko.observable();
    self.Description = ko.observable();
    self.Score = ko.observable();
    self.ProductImg = ko.observable();
    self.Quantity = ko.observable();
    self.QuantityToOrder = ko.observable();
    //current user info
    self.UserName = ko.observable();

    //collections
    self.Products = ko.observableArray();
    self.OrderList = ko.observableArray();
    $.cookie.json = true;

    self.goDetail = function (item) {
        self.QuantityToOrder(0);
        self.PId(item.PId);
        self.Name(item.Name);
        self.Description(item.Description);
        self.Score(item.Score);
        self.Quantity(item.Quantity);
        //var objectNew = $.parseJSON($.cookie('huyanh001'));
        //console.log(objectNew);
        //$.each(objectNew, function (idx, item) {
        //    //console.log('item id : ' + item.PId + ', quantity :' + item.Quantity);
        //    //console.log(objectNew);
        //});
    };

    self.doOrder = function (item) {
        self.OrderList.removeAll();
        var dataToSeri = {
            PId: self.PId,
            Name: self.Name,
            Score: self.Score,
            Quantity: self.Quantity,
            QuantityToOrder: self.QuantityToOrder
        };

        console.log('------');
        if (dataToSeri.QuantityToOrder() < dataToSeri.Quantity()) {
            var flag = 0;
            if (self.UserName() != null) {
                var objectNew = $.parseJSON($.cookie(self.UserName()));
                //console.log(objectNew);
                if (objectNew != null) {
                    $.each(objectNew, function (idx, objectNewItem) {
                        console.log('item id : ' + objectNewItem.PId + ', quantity :' + objectNewItem.QuantityToOrder);
                        if (objectNewItem.PId == dataToSeri.PId()) {
                            flag = 1;
                            //console.log('go flag = 1');
                            objectNewItem.QuantityToOrder = parseInt(objectNewItem.QuantityToOrder) + parseInt(dataToSeri.QuantityToOrder());
                            self.OrderList.push(objectNewItem);
                        } else {
                            //console.log('go flag = 2');
                            //flag = 0;
                            self.OrderList.push(objectNewItem);
                        }
                    });
                    if (flag == 0) {
                        //console.log('go flag = 0');
                        self.OrderList.push(dataToSeri);
                    }
                }
                else {
                    self.OrderList.push(dataToSeri);
                }
                $.cookie(self.UserName(), ko.toJSON(self.OrderList()), { expires: 1, path: '/' });
            } else {
                self.OrderList.push(dataToSeri);
                $.cookie(self.UserName(), ko.toJSON(self.OrderList()), { expires: 1, path: '/' });
            }
        } else {
            toastr.error('Quá số lượng trong kho !');
        }
    }
};


//load data functions
var loadProducts = function (id, searchString) {
    listProductViewModel.Products.removeAll();
    $.ajax({
        url: '/api/MemberProduct/GetProducts/',
        type: 'GET',
        data: { value: searchString },
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    }).done(function (items) {
        $.each(items, function (idx, item) {
            listProductViewModel.Products.push(new zombies.Product(item));
        });
    }).error(function () {
        toastr.error('error');
    })
};

var loadCurrentUser = function () {
    $.ajax({
        url: '/api/MemberProfile/GetCurrentUser',
        type: 'GET',
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    }).done(function (items) {
        if (items != null) {
            listProductViewModel.UserName(items);
        } else {
            toastr.error('Bạn cần phải login !');
        }
    }).error(function () {
        toastr.error('Lỗi bất thường !');
    })
};

//document ready/ Load view model
var listProductViewModel;
$(document).ready(function () {

    listProductViewModel = new zombies.ListProductClient();
    loadProducts();
    loadCurrentUser();

    ko.applyBindings(listProductViewModel, document.getElementById("my-container"));
});